/*
 * @Author: HuangYue
 * @Date: 2022-04-11 15:47:50
 * @LastEditors: HuangYue
 * @LastEditTime: 2022-04-28 01:10:52
 * @FilePath: /graduation/mock/peopleManage.js
 * @Description:人员管理 api
 */
import { randomPhoneNumber, randomName, randomIdcard, randomNickname } from '../src/util/random';
/**
 * @description: 模拟网络请求延时
 * @param {*} time
 * @return {*}
 */
export const waitTime = (time = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};
export let tableListDataSource = [];
export let uidBase = 100000000000;
export const avatar = [
  'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
  'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
  'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
];
const role = ['admin', 'user', 'engineer'];
const emails = ['baba@antfin.com', 'test@antfin.com', 'test2@antfin.com', 'test3@antfin.com'];
const permissions = [
  ['权限1', '权限2'],
  ['权限1', '权限4'],
  ['权限1'],
  ['权限1', '权限4'],
  ['权限1', '权限2', '权限3'],
];
const stautsEnum = {
  0: 'Logout',
  1: 'Approving',
  2: 'Success',
  3: 'Error',
};
for (let i = 0; i < 50; i += 1) {
  tableListDataSource.push({
    uid: `${uidBase + i}`,
    avatar: avatar[Math.floor(Math.random() * 10) % 3],
    role: role[Math.floor(Math.random() * 10) % 3],
    realName: randomName(),
    nickName: randomNickname('Telemedicine-', 4),
    idNumber: randomIdcard(),
    email: emails[Math.floor(Math.random() * 10) % 4],
    phone: randomPhoneNumber(),
    status: stautsEnum[Math.floor(Math.random() * 10) % 4],
    permission: permissions[Math.floor(Math.random() * 10) % 5],
  });
  uidBase += i;
}
/**
 * 获取表格数据
 */
export const getTableData = () => {
  // 1. 如果 tableData 中本来就有数据，那么直接返回
  if (tableListDataSource.length > 0) {
    return tableListDataSource;
  } else {
    // 2. 如果 tableData 中没有数据
    // 那么直接生成
    for (let i = 0; i < 50; i += 1) {
      tableListDataSource.push({
        uid: `${uidBase + i}`,
        avatar: avatar[Math.floor(Math.random() * 10) % 3],
        role: role[Math.floor(Math.random() * 10) % 3],
        realName: randomName(),
        nickName: randomNickname('Telemedicine-', 4),
        idNumber: randomIdcard(),
        email: emails[Math.floor(Math.random() * 10) % 4],
        phone: randomPhoneNumber(),
        status: stautsEnum[Math.floor(Math.random() * 10) % 4],
        permission: permissions[Math.floor(Math.random() * 10) % 5],
      });
      uidBase += i;
    }
    return tableListDataSource;
  }
};
/**
 * 获取表格数据（医院用户）
 */
export let hospitalTableDataSource = [];
export const getTableDataForHospital = () => {
  // 1. 如果 hospitalTableDataSource 中本来就有数据，那么直接返回
  if (hospitalTableDataSource.length > 0) {
    return hospitalTableDataSource;
  } else if (hospitalTableDataSource.length === 0 && tableListDataSource.length > 0) {
    // 2. 如果 hospitalTableDataSource 中没有数据，但 tableListDataSource 中有数据，那么过滤并返回
    hospitalTableDataSource = tableListDataSource.filter((data) => {
      return data.role === 'user';
    });
  } else if (hospitalTableDataSource.length === 0 && tableListDataSource.length === 0) {
    // 3. 如果 hospitalTableDataSource && tableData 中都没有数据，那么先生成 tableListDataSource hospitalTableDataSource
    tableListDataSource = getTableData();
    hospitalTableDataSource = tableListDataSource.filter((data) => {
      return data.role === 'user';
    });
  }
  return hospitalTableDataSource;
};
let engineerTableDataSource = [];
export const getTableDataForEngineer = () => {
  // 1. 如果 hospitalTableDataSource 中本来就有数据，那么直接返回
  if (engineerTableDataSource.length > 0) {
    return engineerTableDataSource;
  } else if (engineerTableDataSource.length === 0 && tableListDataSource.length > 0) {
    // 2. 如果 hospitalTableDataSource 中没有数据，但 tableListDataSource 中有数据，那么过滤并返回
    engineerTableDataSource = tableListDataSource.filter((data) => {
      return data.role == 'engineer';
    });
  } else if (engineerTableDataSource.length === 0 && tableListDataSource.length === 0) {
    // 3. 如果 hospitalTableDataSource && tableData 中都没有数据，那么先生成 tableListDataSource hospitalTableDataSource
    tableListDataSource = getTableData();
    engineerTableDataSource = tableListDataSource.filter((data) => {
      return data.role == 'engineer';
    });
  }
  return engineerTableDataSource;
};

export default {
  'GET /api/getAllPeople': async (req, res) => {
    await waitTime(2000);
    res.send({
      success: true,
      data: tableListDataSource,
      total: tableListDataSource.length,
    });
  },
  'POST /api/deletePeopleByUid': async (req, res) => {
    const { uid } = req.body;
    const temp = [];
    tableListDataSource.forEach((item) => {
      if (item.uid !== uid) {
        temp.push(item);
      }
    });
    tableListDataSource = temp;
    await waitTime(2000);
    res.send({
      success: true,
      data: tableListDataSource,
      total: tableListDataSource.length,
    });
  },
  'POST /api/updatePeople': async (req, res) => {
    const current = req.body;
    const temp = [];
    tableListDataSource.forEach((item) => {
      if (item.uid === current.uid) {
        temp.push(current);
      } else {
        temp.push(item);
      }
    });
    await waitTime(2000);
    tableListDataSource = temp;
    res.send({
      success: true,
      data: tableListDataSource,
      total: tableListDataSource.length,
    });
  },
  'POST /api/addPeople': async (req, res) => {
    const current = req.body;
    tableListDataSource.unshift({ ...current, uid: uidBase, avatar: avatar[0] });
    uidBase += 1;
    await waitTime(2000);
    res.send({
      success: true,
      data: tableListDataSource,
      total: tableListDataSource.length,
    });
  },
};
